// import {} from './constants';
import NetUtil from '../../common/NetUtil';
import config from '../../config/config';
import {setConfigValue, configkey, toast,showLoading,hideLoading} from '../../utils/NativeUtil';

export const getCodeAction = (phoneNum, success) => {
    return dispatch => {
        const params = {
            phone: phoneNum
        };

        let url = '/sms/shop/register';
        showLoading("获取验证码");
        NetUtil.postSign(config.apiUrl + url, params, function (res) {
            hideLoading();
            if (res.success) {
                success();
            } else {
                toast(res.message);
            }
        })
    }
};

//重置密码获取验证码
export const resetCodeAction = (phoneNum, success) => {
    return dispatch => {
        const params = {
            phone: phoneNum
        };

        let url = '/sms/shop/password/reset';
        showLoading("获取验证码");
        NetUtil.postSign(config.apiUrl + url, params, function (res) {
            hideLoading();
            if (res.success) {
                success();
            } else {
                toast(res.message);
            }
        })
    }
};

//注册
export const registerAction = (phoneNum, passWord, Code, success) => {
    return dispatch => {
        const params = {
            phone: phoneNum,
            password: passWord,
            code: Code
        };

        let url = '/register/shop';
        showLoading("正在注册");
        NetUtil.postSign(config.apiUrl + url, params, function (res) {
            hideLoading();
            if (res.success) {
                let data = res.data;
                let user = data.user;
                let token = data.token;
                setConfigValue(configkey.UserToken, token);
                setConfigValue(configkey.LoginState, "1");
                // setConfigValue(configkey.Mobile, user.mobile);
                // setConfigValue(configkey.UserSex, user.sex);
                setConfigValue(configkey.UserName, user.username);
                success();
            } else {
                toast(res.message);
            }
        })
    }
};

//重置密码
export const resetPwdAction = (phoneNum, passWord, Code, success) => {
    return dispatch => {
        const params = {
            phone: phoneNum,
            password: passWord,
            code: Code
        };

        let url = '/user/shop/password/reset';
        showLoading("正在加载");
        NetUtil.postSign(config.apiUrl + url, params, function (res) {
            hideLoading();
            if (res.success) {
                success();
            } else {
                toast(res.message);
            }
        })
    }
};

